/**
 * 是否是手机号码
 * */
function isMobile(mobile) {
    return /^[1][3,4,5,7,8][0-9]{9}$/.test(mobile);
}

/**
 * 是否是邮箱
 * */
function isEmail(email) {
    return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email);
}

/**
 * 加法
 * */
function add(a, b) {
    var c, d, e;
    try {
        c = a.toString().split(".")[1].length;
    } catch (f) {
        c = 0;
    }
    try {
        d = b.toString().split(".")[1].length;
    } catch (f) {
        d = 0;
    }
    return e = Math.pow(10, Math.max(c, d)), (mul(a, e) + mul(b, e)) / e;
}

/**
 * 减法
 * */
function sub(a, b) {
    var c, d, e;
    try {
        c = a.toString().split(".")[1].length;
    } catch (f) {
        c = 0;
    }
    try {
        d = b.toString().split(".")[1].length;
    } catch (f) {
        d = 0;
    }
    return e = Math.pow(10, Math.max(c, d)), (mul(a, e) - mul(b, e)) / e;
}

/**
 * 乘法
 * */
function mul(a, b) {
    var c = 0,
        d = a.toString(),
        e = b.toString();
    try {
        c += d.split(".")[1].length;
    } catch (f) {
    }
    try {
        c += e.split(".")[1].length;
    } catch (f) {
    }
    return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
}

/**
 * 除法
 * */
function div(a, b) {
    var c, d, e = 0,
        f = 0;
    try {
        e = a.toString().split(".")[1].length;
    } catch (g) {
    }
    try {
        f = b.toString().split(".")[1].length;
    } catch (g) {
    }
    return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), mul(c / d, Math.pow(10, f - e));
}

/**
 * 返回距离现在xx天xx小时xx分钟xx秒（依赖moment.js）
 * */
function fromNow(time) {
    var deadline = moment(time);
    var now = moment();
    var diff = moment(deadline.diff(now)) / 1000;     //距离现在多少秒
    var day = Math.floor(diff / (24 * 3600));
    var hour = Math.floor((diff / 3600) % 24);
    var minute = Math.floor((diff / 60) % 60);
    var second = Math.floor(diff % 60);
    return {day: day, hour: hour, minute: minute, second: second};
}

/**
 * 获取购物车(依赖js.cookie.js)
 * */
function getCart() {
    var array = [];
    var carts = Cookies.get('cart');
    if (carts && carts != '') {
        var cartArray = carts.split(',');
        $.each(cartArray, function () {
            var skuId = this.split('-')[0];
            var quantity = this.split('-')[1];
            array.push({skuId : skuId, quantity : quantity});
        });
    }
    return array;
}

function setCart(array) {
    var value = '';
    $.each(array, function() {
        value += this.skuId + '-' + this.quantity + ','
    });
    if(value != '') {
        value = value.substring(0, value.length - 1);
    }
    Cookies.set('cart', value, {expires : 365});
}

/**
 * 增量添加购物车(依赖js.cookie.js)
 * */
function addCart(skuId, quantity) {
    if(skuId && quantity && parseInt(skuId) == skuId && parseInt(quantity) == quantity) {
        var cartArray = getCart();
        var has = false;
        $.each(cartArray, function () {
            if (skuId == this.skuId) {
                this.quantity = Number(this.quantity) + Number(quantity);
                has = true;
                return false;
            }
        });
        if (has) {
            setCart(cartArray);
        } else {
            if (quantity > 0) {
                cartArray.push({skuId: skuId, quantity: quantity});
                setCart(cartArray);
            }
        }
    }
}

function removeCart(skuId) {
    if(skuId && parseInt(skuId) == skuId) {
        var cartArray = getCart();
        var removeIndex = -1;
        $.each(cartArray, function (i, v) {
            if (skuId == v.skuId) {
                removeIndex = i;
                return false;
            }
        });
        if (removeIndex >= 0) {
            cartArray.splice(removeIndex, 1);
            setCart(cartArray);
        }
    }
}